﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace DAL
{
    public class EmployeeDAL
    {
        public static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();

        #region 实现登录的方法
        /// <summary>
        /// 实现登录的方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public T Login<T>(string name, string pwd)
        {
            return Common.Login<T>(name, pwd);
        }
        #endregion

        #region 添加勘察信息
        /// <summary>
        /// 添加勘察信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Add<T>(T t)
        {
            return Common.ADD<T>(t);
        }
        #endregion

        #region 根据项目号修改项目状态和勘察开始时间
        /// <summary>
        /// 根据项目号修改项目状态和勘察开始时间
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Update(string ProjectNumber)
        {
            return DBHelper.ExecuteNonQuery(string.Format("UPDATE EABasic_Info SET Basic_State=8,Survey_StartTime=getDate() WHERE ProjectNumber ={0}", ProjectNumber));
        }
        #endregion

        #region 批量添加图片
        /// <summary>
        /// 批量添加图片
        /// </summary>
        /// <param name="photo"></param>
        /// <returns></returns>
        public int Upload(EAPhoto photo)
        {
            string PhotoName = photo.Photo_Name;
            string[] Name = PhotoName.Split(',');

            string PhotoUrl = photo.Photo_Url;
            string[] url = PhotoUrl.Split('=');

            StringBuilder bui = new StringBuilder();

            if (Name.Length > 0 && url.Length > 0)
            {
                foreach (var item in Name)
                {
                    foreach (var item1 in url)
                    {
                        bui.Append("insert into EAPhoto(ProjectNumber,Photo_Name,Photo_Url) values('" + photo.ProjectNumber + "','" + item + "','" + item1 + "=" + "')");
                        break;
                    }

                }
            }
            return DBHelper.ExecuteNonQuery(bui.ToString());
        }
        #endregion

        #region 审核修改状态
        /// <summary>
        /// 审核修改状态
        /// </summary>
        /// <param name="Auditing_Type"></param>
        /// <param name="ProjectNumber"></param>
        /// <returns></returns>
        public int ExamineUpdate(int Basic_State, string ProjectNumber)
        {
            return DBHelper.ExecuteNonQuery("update [dbo].[EABasic_Info] set [Basic_State]='" + Basic_State + "' where ProjectNumber='" + ProjectNumber + "'");
        }
        #endregion

        #region 审核修改状态不成功时删除审核表中的之前添加的语句
        /// <summary>
        /// 审核修改状态不成功时删除审核表中的之前添加的语句
        /// </summary>
        /// <param name="ProjectNumber"></param>
        /// <returns></returns>
        public int ExamineDelete(string ProjectNumber)
        {
            return DBHelper.ExecuteNonQuery("delete from [dbo].[EAAuditing] where ProjectNumber=" + ProjectNumber);
        }
        #endregion

        #region 勘察页面的 查看详情
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <returns></returns>
        public DengJi BackDatas(string ProjectNumber)
        {
            string SqlText = @"select *
                            from EABasic_Info a  join EALoan_Info c
                            on a.ProjectNumber=c.ProjectNumber
                            where a.ProjectNumber='" + ProjectNumber + "'"; ;
            Type type = typeof(DengJi);
            var SelectField = string.Join(",", type.GetProperties().Select(p => string.Format("[{0}]", p.Name)));
            SqlText = string.Format(SqlText, SelectField, type.Name);
            DengJi dj = new DengJi();
            using (SqlConnection con = new SqlConnection(connString))
            {
                con.Open();
                SqlCommand com = new SqlCommand(SqlText, con);
                com.Parameters.Add(new SqlParameter("@ProjectNumber", ProjectNumber));
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    foreach (var pop in type.GetProperties())
                    {
                        object oValue = reader[pop.Name];
                        if (oValue is DBNull)
                        {
                            pop.SetValue(dj, null);
                        }
                        else
                        {
                            pop.SetValue(dj, oValue);
                        }
                    }
                }
            }
            return dj;
        }
        #endregion

    }
}
